const Koa = require('koa')
const mongoose = require('mongoose')
const routeUse = require('./routes/index')
const koaBody = require('koa-body');
const error = require('koa-json-error')
const parameter = require('koa-parameter')
const path = require("path")

const app = new Koa()

mongoose.connect('mongodb://darly:946513@localhost:27017/blog?authSource=admin', {
    useNewUrlParser: true,
    useUnifiedTopology: true
})


mongoose.connection.on('error', console.error)

app.use(async(context, next) => {
    context.set("Access-Control-Allow-Origin", "*")
    context.set("Content-Type", "application/json; charset=utf-8")
    context.set("Access-Control-Allow-Credentials", true)
    context.set("Access-Control-Allow-Headers", "Content-Type")

    await next()
})
app.use(koaBody({
    multipart: true,
    formidable: {
        uploadDir: "/home/files",
        keepExtensions: true,
        maxFileSize: 1048576
    }
}));
app.use(error({
    postFormat: (err, { stack, ...rest }) => rest // 不返回stack: 堆栈信息
}))
app.use(parameter(app))

routeUse(app)

app.listen(3002)
console.log('start 3002');